//
//  ZYCurveViewModel.m
//  YiLin
//
//  Created by Zyark on 2018/7/10.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYCurveViewModel.h"
#import "ZYCurve.h"
#import "AFNetWorkUtils.h"

@interface ZYCurveViewModel()
/**
 获取曲线
 */
@property(nonatomic,strong) RACCommand *curveCommand;

/**设置自定义曲线*/
@property(nonatomic,strong) RACCommand *setCustomCurveCommand;

/**获取自定义曲线*/
@property(nonatomic,strong) RACCommand *getCustomCurveCommand;
@end

@implementation ZYCurveViewModel

-(RACCommand *)curveCommand{
    if(!_curveCommand){
        _curveCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_CURVE_URL];
                NSDictionary *params;
                params = @{@"ssid":[ZYConfig loginToken]};
                [[AFNetWorkUtils racGETWithURL:url params:params] subscribeNext:^(id x) {
                    NSArray *array = [x objectForKey:@"result"];
                    NSArray *result = [NSArray modelArrayWithClass:[ZYCurve class] json:array];
                    [subscriber sendNext:result];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _curveCommand;
}

-(RACCommand *)setCustomCurveCommand{
    if(!_setCustomCurveCommand){
        _setCustomCurveCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_SET_CUSTOMIZE_CURVE_URL];
                NSString *errorInfo = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_SET_CURVE_ERROR];
                if(!self.valueArray || [self.valueArray count] != 24){
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:errorInfo]];
                    [subscriber sendCompleted];
                }
                for (NSString *itemValue in self.valueArray) {
                    if(NULLString(itemValue)){
                        [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:errorInfo]];
                        [subscriber sendCompleted];
                    }
                }
                NSDictionary *params;
                NSString *value = [self.valueArray componentsJoinedByString:@","];
                params = @{@"ssid":[ZYConfig loginToken],@"macAddress":self.macAddress,@"value":value};
                [[AFNetWorkUtils racPOSTWithURL:url params:params] subscribeNext:^(id x) {
                    [subscriber sendNext:[NSNumber numberWithBool:YES]];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _setCustomCurveCommand;
}

-(RACCommand *)getCustomCurveCommand{
    if(!_getCustomCurveCommand){
        @weakify(self);
        _getCustomCurveCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_CUSTOMIZE_CURVE_URL];
                NSDictionary *params;
                params = @{@"ssid":[ZYConfig loginToken],@"macAddress":self.macAddress};
                [[AFNetWorkUtils racGETWithURL:url params:params] subscribeNext:^(id x) {
                    NSDictionary *result = [x objectForKey:@"result"];
                    ZYCurve *curve = [ZYCurve modelWithJSON:result];
                    [subscriber sendNext:curve];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _getCustomCurveCommand;
}

@end
